<template>
  <div>
    <ConTitle :info="topTitle"/>
    <!--    搜索-->
    <el-card class="box-card card-top">
      <div class="clearfix card-con">
        <el-form ref="form" :model="query" label-width="80px" :inline="true">
          <el-form-item label="群组名称:" class="label-width">
            <el-input v-model="query.keyword" placeholder="群组名称"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" size="mini" icon="el-icon-search" @click="onSubmit">搜索</el-button>
            <span>搜索结果:{{ total }}</span>
          </el-form-item>
        </el-form>
        <el-button style="float: right" type="primary" @click="addDialog">新增群组 ＋</el-button>
      </div>
    </el-card>
    <!--    表格-->
    <el-table :data="groupList" style="width: 100%">
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column prop="groupName" label="群组名称"></el-table-column>
      <el-table-column prop="nodeNums" label="传感器节点数量"></el-table-column>
      <el-table-column prop="onlineNums" label="在线设备数量"></el-table-column>
      <el-table-column prop="cTime" label="创建时间"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="showDialog(scope.row.id, scope.row.groupName)" type="text">重命名</el-button>
          <el-button type="text" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--    分页-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.currPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total" class="pagination">
    </el-pagination>

    <!--    弹窗 新增或修改-->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="群组名称" prop="groupName">
          <el-input v-model="ruleForm.groupName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="handleOk">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import ConTitle from "@/components/ConTitle.vue";
import {$groupList, $groupAdd, $groupEdit, $groupDel} from "@/api/index.js";

export default {
  name: "group",
  components: {
    ConTitle,
  },
  data() {
    return {
      topTitle: {
        type: "设备",
        text: "节点群组",
        msg: "以分组的形式管理节点设备,提供了搜索,添加新的群组,重命名,移动设备等功能.",
      },
      groupList: [],
      query: {
        //搜索和分页的数据
        currPage: 1,//当前页
        pageSize: 5,//每页条数
        keyword: "",//群组名称, api接口文档不完善，需要根后台程序员沟通
      },
      total: 0,//总数量
      dialogVisible: false,//弹窗是否显示 false 不显示 ；true 显示
      ruleForm: {groupName: ""}, //添加或修改时，群组名称
      rules: {
        //做验证处理 validate
        groupName: [
          {required: true, message: "请输入群组名称", trigger: "blur"},
        ],
      },
      editId: "",//要修改的数据id
    };
  },
  created() {
    this.getGroupList();
  },
  methods: {
    //删除
    handleDel(id) {
      console.log(id);
//删除前需要确认
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            $groupDel({id: id}).then((res) => {
              console.log(res);
              if (res.data.code == 666) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.query.currPage = 1;
                //重新渲染页面
                this.getGroupList();
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
    },
    //显示添加接口
    addDialog() {
      console.log("添加");
      this.dialogVisible = true;
      this.editId = "";
      this.ruleForm.groupName = "";
    },
    //显示修改窗口，并 把群组信息带过去
    showDialog(id, groupName) {
      console.log("重命名");
      this.dialogVisible = true;
      this.editId = id;
      this.ruleForm.groupName = groupName;
    },
    //点击确定按钮
    handleOk() {
      //验证 表单提交的数据是否符合验证条件
      console.log(11);
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          //判断是添加还是修改
          if (this.editId !== "") {
            this.editItem()//修改
          } else {
            this.addItem();//添加
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
// 添加
    addItem() {
      //添加接口
      $groupAdd({groupName: this.ruleForm.groupName}).then((res) => {
        console.log(res);
        //1.判断是否添加成功
        if (res.data.msg == "success") {
          //2.关闭弹窗
          this.dialogVisible = false;
          //3.重新调用列表接口
          this.getGroupList();
        }
      });
    },
    // 修改
    editItem() {
      $groupEdit({id: this.editId, groupName: this.ruleForm.groupName}).then(
          (res) => {
            console.log(res);
            //1.修改成功
            if (res.data.msg == "success") {
              //2.关闭弹窗
              this.dialogVisible = false;
              //3.重新调用列表接口
              this.getGroupList();
            }
          }
      );
    },
    // 搜索
    onSubmit() {
      this.query.currPage = 1;
      //重新获取数据
      this.getGroupList();
    },
    //获取列表数据的函数
    getGroupList() {
      $groupList(this.query).then((res) => {
        console.log(res);
        this.groupList = res.data.data;
        this.total = res.data.total;
      });
    },
    //修改分页的每页显示条数
    handleSizeChange(num) {
      console.log("显示条数", num);
      this.query.pageSize = num;
      this.query.currPage = 1;
      //重新获取数据
      this.getGroupList();
    },
    //修改当前页页码
    handleCurrentChange(num) {
      console.log("当前页", num);
      //将当前页的页码赋值给 currPage
      this.query.currPage = num;
      //重新获取数据
      this.getGroupList();
    },
  },
};
</script>

<style scoped lang="scss">
.card-con {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.pagination {
  text-align: center;
  background: white;
}
</style>